<script setup>
import {onMounted, ref} from "vue";
import {ListMessages, Messages, PutResume, SelectJob} from "@/utils/api/graduate.js";
import {ElMessage, ElNotification} from "element-plus";
import {useUserLoginStore} from "@/stores/AccountLogin.js";

const seEnterprise = ref("");
const seOther = ref("");
const sno = useUserLoginStore().getAccount;
const data = ref([{
  eno: '',
  enterpriseName: '',
  id: '',
  jobInfo: '',
  announceTime: '',
}]);
const visibleTo = ref(false);
const toMsg = ref({
  sno: '',
  eno: '',
  content: '',
  jobId: '',
});
const visibleLt = ref(false);
const ltMsg = ref([{
  content: '',
  respond: '',
  contentTime: '',
  respondTime: '',
}]);

const selectJob = () => {
  data.value = [];
  SelectJob(seEnterprise.value,  seOther.value)
      .then(result => {
        if (result.code === 1) {
          data.value = result.data;
        } else {
          ElMessage({
            message: result.message,
            type: 'warning'
          });
        }
      }).catch(error => console.error(error.toString()));
}

const putResume = (eno, jobId) => {
  PutResume(sno, eno, jobId).then(result => {
    if (result.code === 1) {
      ElNotification({
        title: '投递成功',
        type: 'success',
        duration: 2000,
      })
    } else {
      ElMessage({
        message: "重复投递，或投递失败。若失败请先更新简历",
        type: 'warning',
      })
    }
  }).catch(error => console.error(error.toString()));
}

const openMsg = (eno, jobId) => {
  visibleTo.value = !visibleTo.value;
  toMsg.value.sno = sno;
  toMsg.value.eno = eno;
  toMsg.value.jobId = jobId;
}

const messages = () => {
  const m = toMsg.value;
  Messages(m.sno, m.eno, m.content, m.jobId).then(result => {
    if (result.code === 1) {
      ElNotification({
        message: '留言成功',
        type: 'success',
      })
    } else {
      ElMessage({
        message: "留言失败，请重试",
        type: 'warning',
      })
    }
  }).catch(error => console.error(error.toString()));
  visibleTo.value = !visibleTo.value;
  m.content = '';
}

const listMsg = (eno, jobId) => {
  visibleLt.value = !visibleLt.value;
  ListMessages(sno, eno, jobId).then(result => {
    if (result.code === 1) {
      ltMsg.value = result.data;
    } else {
      ElMessage({
        message: "服务器错误，请重试",
        type: 'error',
      })
    }
  }).catch(error => console.error(error.toString()));
}

onMounted(() => {
  selectJob();
})
</script>

<template>
  <el-form label-width="100px" label-position="top" inline>
    <el-form-item label="企业名称">
      <el-input v-model="seEnterprise" placeholder="单位地址"/>
    </el-form-item>
    <el-form-item label="职位关键词">
      <el-input v-model="seOther" placeholder="职位关键词"/>
    </el-form-item>
    <el-form-item label="-">
      <el-button type="primary" @click="selectJob">搜索</el-button>
    </el-form-item>
  </el-form>

  <div class="result">
    <div v-for="(item, index) in data" :key="index">
      <el-descriptions direction="horizontal" border :column="4">
        <el-descriptions-item label="企业名称">{{ item.enterpriseName }}</el-descriptions-item>
        <el-descriptions-item label="企业编号">{{ item.eno }}</el-descriptions-item>
        <el-descriptions-item label="职位编号">{{ item.id }}</el-descriptions-item>
        <el-descriptions-item label="发布时间">{{ item.announceTime }}</el-descriptions-item>
        <el-descriptions-item label="招聘信息">
          {{ item.jobInfo }}<br>
          <el-button link type="success" @click="putResume(item.eno,item.id)">投 递 简 历</el-button>
          <el-button link type="primary" @click="openMsg(item.eno, item.id)">留 言</el-button>
          <el-button link type="danger" @click="listMsg(item.eno,item.id)">查 看 留 言</el-button>
        </el-descriptions-item>
      </el-descriptions>
    </div>
  </div>

  <el-dialog v-model="visibleTo" title="留言内容" width="600" @close="toMsg.content = '';">
    <el-input type="textarea" :rows="6" v-model="toMsg.content" resize="none"
              maxlength="245" :show-word-limit="true"/>
    <template #footer>
      <el-button type="success" @click="messages">保 存</el-button>
    </template>
  </el-dialog>

  <el-dialog v-model="visibleLt" title="查看留言" width="800">
    <div class="msg-info">
      <div v-for="(item, index) in ltMsg" :key="index">
        <div class="msg-content" v-if="item.content !== null && item.content !== ''">
          留言-> {{ item.content }}<span class="time">__{{ item.contentTime }}</span>
        </div>
        <div class="msg-respond" v-if="item.respond !== null && item.respond !== ''">
          {{ item.respond }} <-回复<span class="time">__{{ item.respondTime }}</span>
        </div>
      </div>
    </div>
  </el-dialog>

</template>

<style scoped>
.result {
  min-width: 1100px;
  padding: 1px 10px 10px 1px;
  overflow-y: auto;
  max-height: 550px;
}

.el-descriptions {
  min-width: 1000px;
  width: 1000px;
  margin-top: 10px;
}

.msg-info {
  max-height: 500px;
  overflow: auto;
}

.time {
  color: gray;
}

.msg-content {
  text-align: left;
  color: green;
}

.msg-respond {
  text-align: right;
  color: blue;
}

</style>
